package cn.com.bluemoon.daps.model.core.file;

import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.exception.DapException;
import cn.com.bluemoon.daps.model.core.file.hive.HiveFieldData;
import cn.com.bluemoon.daps.model.core.file.hive.HivePartitionData;
import cn.com.bluemoon.daps.model.core.file.hive.HiveTableData;
import cn.com.bluemoon.daps.model.core.file.mysql.MySqlFieldData;
import cn.com.bluemoon.daps.model.core.file.mysql.MySqlIndexData;
import cn.com.bluemoon.daps.model.core.file.mysql.MySqlTableData;
import cn.com.bluemoon.daps.model.core.file.pgsql.PgSqlFieldData;
import cn.com.bluemoon.daps.model.core.file.pgsql.PgSqlIndexData;
import cn.com.bluemoon.daps.model.core.file.pgsql.PgSqlTableData;

import java.util.Optional;

/**
 * 数据库类型+type(表、字段、索引、分区）进行返回对应的实例类
 *
 * @author Jarod Kong
 * @date 2021-03-20 14:02
 **/
@SuppressWarnings("rawtypes")
public class MetadataFatory {
    public static Optional<IMetaData> use(DatabaseType dbType, Type type) {
        return Optional.of(use0(dbType, type));
    }

    public static Class<? extends IMetaData> useClz(DatabaseType dbType, Type type) {
        return use0(dbType, type).getClass();
    }

    public static IMetaData use0(DatabaseType dbType, Type type) {
        switch (dbType) {
            case PGSQL:
                if (Type.TABLE.equals(type)) {
                    return PgSqlTableData.getInstance();
                } else if (Type.FIELD.equals(type)) {
                    return PgSqlFieldData.getInstance();
                } else if (Type.INDEX.equals(type)) {
                    return PgSqlIndexData.getInstance();
                }
                break;
            case MYSQL:
                if (Type.TABLE.equals(type)) {
                    return MySqlTableData.getInstance();
                } else if (Type.FIELD.equals(type)) {
                    return MySqlFieldData.getInstance();
                } else if (Type.INDEX.equals(type)) {
                    return MySqlIndexData.getInstance();
                }
                break;
            case HIVE:
                if (Type.TABLE.equals(type)) {
                    return HiveTableData.getInstance();
                } else if (Type.FIELD.equals(type)) {
                    return HiveFieldData.getInstance();
                } else if (Type.PARTITION.equals(type)) {
                    return HivePartitionData.getInstance();
                }
                break;
            default:
                throw new DapException(dbType + "获取对应" + type + "实例失败");
        }
        throw new DapException(dbType + "获取对应" + type + "实例失败");
    }

    /**
     * 类型
     */
    public enum Type {
        /**
         * 表、字段、索引、分区
         */
        TABLE, FIELD, INDEX, PARTITION
    }
}
